﻿using System.IO;

namespace Koala.Pro.Common
{
    public class DirectoryUtil
    {
        public static bool IsWriteable(string path)
        {
            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch
                {
                    return false;
                }
            }
            try
            {
                string path2 = ".test." + Guid.NewGuid().ToString().Substring(0, 5);
                string path3 = Path.Combine(path, path2);
                File.WriteAllLines(path3, new string[1] { "test" });
                File.Delete(path3);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool IsDiskSpaceEnough(string path, ulong requiredSpace)
        {
            ulong freeSpace = GetFreeSpace(Path.GetPathRoot(path));
            return requiredSpace <= freeSpace;
        }

        public static ulong GetFreeSpace(string driveName)
        {
            ulong result = 0uL;
            try
            {
                result = (ulong)new DriveInfo(driveName).AvailableFreeSpace;
            }
            catch
            {
            }
            return result;
        }

        public static ulong ConvertByteCountToKByteCount(ulong byteCount)
        {
            return byteCount / 1024;
        }

        public static float ConvertKByteCountToMByteCount(ulong kByteCount)
        {
            return kByteCount / 1024;
        }

        public static float ConvertMByteCountToGByteCount(float kByteCount)
        {
            return kByteCount / 1024f;
        }

        public static string[] GetFileNames(string directoryPath)
        {
            if (!IsExistDirectory(directoryPath))
            {
                throw new FileNotFoundException();
            }
            return Directory.GetFiles(directoryPath);
        }

        public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
        {
            if (!IsExistDirectory(directoryPath))
            {
                throw new FileNotFoundException();
            }
            try
            {
                if (isSearchChild)
                {
                    return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
                }
                return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        public static List<FileInfo> GetAllFiles(string directoryPath, List<FileInfo> data = null)
        {
            if (!IsExistDirectory(directoryPath))
            {
                return new List<FileInfo>();
            }
            List<FileInfo> list = ((data == null) ? new List<FileInfo>() : data);
            DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
            DirectoryInfo[] directories = directoryInfo.GetDirectories();
            FileInfo[] files = directoryInfo.GetFiles();
            if (files.Length != 0)
            {
                list.AddRange(files);
            }
            DirectoryInfo[] array = directories;
            for (int i = 0; i < array.Length; i++)
            {
                GetAllFiles(array[i].FullName, list);
            }
            return list;
        }

        public static string[] GetDirectories(string directoryPath)
        {
            try
            {
                return Directory.GetDirectories(directoryPath);
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
        {
            try
            {
                if (isSearchChild)
                {
                    return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories);
                }
                return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        public static string CreateDirectoryByDate(string rootPath)
        {
            return CreateDirectoryByDate(rootPath, "yyyy-MM-dd");
        }

        public static string CreateDirectoryByDate(string rootPath, string formatString)
        {
            if (!IsExistDirectory(rootPath))
            {
                throw new DirectoryNotFoundException("the rootPath is not found");
            }
            bool flag = false;
            flag = !(formatString == "yyyy-MM-dd") && formatString == "yyyy-MM-dd-HH";
            string text = rootPath + "\\" + DateTime.Now.Year;
            CreateDirectory(text);
            text = text + "\\" + DateTime.Now.Month.ToString("00");
            CreateDirectory(text);
            text = text + "\\" + DateTime.Now.Day.ToString("00");
            CreateDirectory(text);
            if (flag)
            {
                text = text + "\\" + DateTime.Now.Hour.ToString("00");
                CreateDirectory(text);
            }
            return text;
        }

        public static void AssertDirExist(string filePath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
            if (!directoryInfo.Exists)
            {
                directoryInfo.Create();
            }
        }

        public static void CreateIfNotExists(string directory)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }

        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }

        public static bool IsEmptyDirectory(string directoryPath)
        {
            try
            {
                if (GetFileNames(directoryPath).Length != 0)
                {
                    return false;
                }
                if (GetDirectories(directoryPath).Length != 0)
                {
                    return false;
                }
                return true;
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        public static bool ContainFile(string directoryPath, string searchPattern)
        {
            try
            {
                if (GetFileNames(directoryPath, searchPattern, isSearchChild: false).Length == 0)
                {
                    return false;
                }
                return true;
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        public static bool ContainFile(string directoryPath, string searchPattern, bool isSearchChild)
        {
            try
            {
                if (GetFileNames(directoryPath, searchPattern, isSearchChild: true).Length == 0)
                {
                    return false;
                }
                return true;
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        public static void CopyFolder(string varFromDirectory, string varToDirectory)
        {
            Directory.CreateDirectory(varToDirectory);
            if (!Directory.Exists(varFromDirectory))
            {
                return;
            }
            string[] directories = Directory.GetDirectories(varFromDirectory);
            if (directories.Length != 0)
            {
                string[] array = directories;
                foreach (string text in array)
                {
                    CopyFolder(text, varToDirectory + text.Substring(text.LastIndexOf("\\")));
                }
            }
            string[] files = Directory.GetFiles(varFromDirectory);
            if (files.Length != 0)
            {
                string[] array = files;
                foreach (string text2 in array)
                {
                    File.Copy(text2, varToDirectory + text2.Substring(text2.LastIndexOf("\\")), overwrite: true);
                }
            }
        }

        public static void CreateDirectory(string directoryPath)
        {
            if (!IsExistDirectory(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
        }

        public static void ClearDirectory(string directoryPath)
        {
            if (IsExistDirectory(directoryPath))
            {
                string[] fileNames = GetFileNames(directoryPath);
                for (int i = 0; i < fileNames.Length; i++)
                {
                    FileUtil.DeleteFile(fileNames[i]);
                }
                string[] directories = GetDirectories(directoryPath);
                for (int j = 0; j < directories.Length; j++)
                {
                    DeleteDirectory(directories[j]);
                }
            }
        }

        public static void DeleteDirectory(string directoryPath)
        {
            if (IsExistDirectory(directoryPath))
            {
                Directory.Delete(directoryPath, recursive: true);
            }
        }

        public static void DeleteDirectory(DirectoryInfo directoryInfo)
        {
            if (!directoryInfo.Exists)
            {
                return;
            }
            FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos();
            foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
            {
                if (fileSystemInfo is FileInfo)
                {
                    FileInfo obj = (FileInfo)fileSystemInfo;
                    obj.Attributes = FileAttributes.Normal;
                    obj.Delete();
                }
                else if (fileSystemInfo is DirectoryInfo)
                {
                    DirectoryInfo obj2 = (DirectoryInfo)fileSystemInfo;
                    obj2.Attributes = FileAttributes.Normal;
                    DeleteDirectory(obj2);
                }
            }
            directoryInfo.Attributes = FileAttributes.Normal;
            directoryInfo.Delete();
        }

        public static void DeleteFolderFiles(string varFromDirectory, string varToDirectory)
        {
            Directory.CreateDirectory(varToDirectory);
            if (!Directory.Exists(varFromDirectory))
            {
                return;
            }
            string[] directories = Directory.GetDirectories(varFromDirectory);
            if (directories.Length != 0)
            {
                string[] array = directories;
                foreach (string text in array)
                {
                    DeleteFolderFiles(text, varToDirectory + text.Substring(text.LastIndexOf("\\")));
                }
            }
            string[] files = Directory.GetFiles(varFromDirectory);
            if (files.Length != 0)
            {
                string[] array = files;
                foreach (string text2 in array)
                {
                    File.Delete(varToDirectory + text2.Substring(text2.LastIndexOf("\\")));
                }
            }
        }

        public static string GetSystemDirectory()
        {
            return Environment.SystemDirectory;
        }

        public static string GetSpeicalFolder(Environment.SpecialFolder folderType)
        {
            return Environment.GetFolderPath(folderType);
        }

        public static string GetTempPath()
        {
            return Path.GetTempPath();
        }

        public static string GetCurrentDirectory()
        {
            return Directory.GetCurrentDirectory();
        }

        public static void SetCurrentDirectory(string path)
        {
            Directory.SetCurrentDirectory(path);
        }

        public static char[] GetInvalidPathChars()
        {
            return Path.GetInvalidPathChars();
        }

        public static DriveInfo[] GetAllDrives()
        {
            return DriveInfo.GetDrives();
        }
    }
}
